# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=f2c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=20210928
pkgrel=2
pkgdesc='Fortran to C code translator (mingw-w64)'
url='https://www.netlib.org/f2c/index.html'
license=('custom')
makedepends=('unzip' "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' 'staticlibs')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
options=('staticlibs')
source=(f2c-src.tgz::"https://www.netlib.org/f2c/src.tgz"
        libf2c-${pkgver}.zip::"https://www.netlib.org/f2c/libf2c.zip"
        20051004-add-ofiles-dep.patch
        20090407-cygwin-build.patch
        libf2c-20110801-64bit-long.patch
        libf2c-1.patch)
noextract=("libf2c-${pkgver}.zip")
sha256sums=('d4847456aa91c74e5e61e2097780ca6ac3b20869fae8864bfa8dcc66f6721d35'
            'ca404070e9ce0a9aaa6a71fc7d5489d014ade952c5d6de7efb88de8e24f2e8e0'
            '3f688297b45515c1e386bb7f94ad36bf0f6a3497a0baff759b54d943cbec7d8f'
            '7fdf61868985bd116307e0039967666845c74c09dc3a3293974ff34d374ac9c6'
            '919698643f12b2fc4b9f4ebac395a8b2c9c01e83028907621b95dee8db4d5653'
            '0c11e2680eaaeb24a2959681ee3b10be8c207b88d0cf0fe354f523fc97a9fff0')

prepare() {
  cd "${srcdir}"
  [[ -d f2c ]] && rm -rf f2c
  mv src f2c

  [[ -d libf2c ]] && rm -rf libf2c
  mkdir libf2c
  unzip -q libf2c-${pkgver}.zip -d libf2c

  # mxe patch
  pushd libf2c
    patch -p1 -i "${srcdir}"/20051004-add-ofiles-dep.patch
    patch -p1 -i "${srcdir}"/20090407-cygwin-build.patch
    patch -p1 -i "${srcdir}"/libf2c-20110801-64bit-long.patch
    patch -p1 -i "${srcdir}"/libf2c-1.patch
  popd
}

build() {
  cd "${srcdir}"
  pushd libf2c
    local _ar="AR=${MINGW_PREFIX}/bin/ar"
    local _ld="LD=${MINGW_PREFIX}/bin/ld"
    local _ranlib="RANLIB=${MINGW_PREFIX}/bin/ranlib"
    make -f makefile.u ${_ar} ${_ld} ${_ranlib} CFLAGS="${CFLAGS} -std=gnu89 -DUSE_CLOCK"
  popd

  cd ${srcdir}/f2c
  make f2c -f makefile.u CFLAGS="${CFLAGS} -std=gnu89"
}

package() {
  cd "${srcdir}/libf2c"
  install -Dm644 f2c.h "${pkgdir}"${MINGW_PREFIX}/include/f2c.h
  install -Dm644 libf2c.a "${pkgdir}"${MINGW_PREFIX}/lib/libf2c.a
  # ${_arch}-strip --strip-unneeded "${pkgdir}"${MINGW_PREFIX}/bin/*.dll
  # ${_arch}-strip -g "${pkgdir}"${MINGW_PREFIX}/lib/*.a

  install -Dm755 ${srcdir}/f2c/f2c.exe "${pkgdir}"${MINGW_PREFIX}/bin/f2c.exe
}
